import {
  API
} from "../../utils/const"
import {
  getRequest
} from "../../utils/request"
import {
  formatTime
} from "../../utils/dateUtils"
// pages/myHistory/myHistory.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabData: ['领用', '回收', '消毒', "报废", "丢失"],
    currentTab: '领用',
    tabMapper: {
      '领用': "USE",
      "回收": "RECYCLE",
      "消毒": "DISINFECT",
      "报废": "DISCARD",
      "丢失": "LOSS",
    },
    tabNameMapper: {
      "USE": '领用',
      "RECYCLE": "回收",
      "DISINFECT": "消毒",
      "DISCARD": "报废",
      "LOSS": "丢失",
    },
    dataList: [],
    dataKeys: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const useCode = this.data.tabMapper[this.data.currentTab];
    console.log(useCode);
    this.requestData(useCode);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  requestData(useCode) {
    getRequest(API.useHistoryTime + useCode).then((res) => {
      for (let key in res) {
        res[key].forEach(v => {
          v.timeStr = formatTime(v.createTime, 'hh-mm-ss')
        })
      }
      this.setData({
        dataList: res,
        dataKeys: Object.keys(res)
      })

    })
  },
  fetchHistory(e) {
    const typeName = e.currentTarget.dataset.type;
    const useCode = this.data.tabMapper[typeName];
    // console.log(useCode);
    this.setData({
      currentTab: typeName
    })
    this.requestData(useCode)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})